
package tetris.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;

import tetris.board.Board;
import tetris.pieces.AbstractShape;

/**
 * The GUI class of the Tetris board.
 * 
 * @author Agari NLN (agarin@uw.edu)
 * @version 2.8
 *
 */
public class BoardGui extends JPanel implements Observer
{
  /**
   * This is here to avoid warnings.
   */
  private static final long serialVersionUID = 1L;
  
  /**
   * The default width of the panel.
   */
  private static final int PANEL_WIDTH = 100;
  
  /**
   * The default height of the panel.
   */
  private static final int PANEL_HEIGHT = 200;
  
  /**
   * The number of how many times a piece(represented by points) are being magnified
   * for eye to see.
   */
  private static final int MAGNIFIER = 10;
  
  /**
   * The Tetris board.
   */
  private final Board my_tetris_board;
  

  /**
   * Constructing an Tetris board UI.
   * @param the_board my Tetris board
   */
  public BoardGui(final Board the_board)
  {
    super();
    /*setPreferredSize(new Dimension(the_gui.getSize().width / 2,the_gui.getSize().height));*/
    setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
    setBackground(Color.WHITE);
    my_tetris_board = the_board;
    my_tetris_board.addObserver(this);
    this.setFocusable(false);
    repaint();
  }

  /**
   * Draw the Tetris board and also the falling piece.
   * @param the_graphics the graphic where the drawn pieces are at.
   */
  public void paintComponent(final Graphics the_graphics)
  {
    super.paintComponent(the_graphics);
    final Graphics2D g2d = (Graphics2D) the_graphics;
    final AbstractShape shape = my_tetris_board.getCurrentPiece();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(shape.getColor());

    final Point[] shapePositions = shape.blockPositions();
    ArrayList<Point> boardPositions = my_tetris_board.pointsOnBoard();

    for (int i = 0; i < shapePositions.length; i++)
    {
      g2d.fillRect((shapePositions[i].x) * MAGNIFIER, 
                   (shapePositions[i].y) * MAGNIFIER, MAGNIFIER, MAGNIFIER);
      repaint();
    }

    for (int i = 0; i < boardPositions.size(); i++)
    {
      g2d.setColor(my_tetris_board.getBoardColor(boardPositions.get(i).x, 
                                                 boardPositions.get(i).y));
      g2d.fillRect((boardPositions.get(i).x) * MAGNIFIER, 
                   (boardPositions.get(i).y) * MAGNIFIER, MAGNIFIER, MAGNIFIER);
      repaint();
    }

    //Draw the boarders
    g2d.setColor(Color.BLACK);
    g2d.drawRect(0, 0, PANEL_WIDTH, PANEL_HEIGHT);
      
    repaint();
  }


  @Override
  public void update(final Observable the_obj, final Object the_arg)
  {
    //System.out.println("update called by " + the_obj);
   // System.out.println(my_tetris_board.getLinesCleared());
  }
}
